<?php
// 使用新浪云storage
use sinacloud\sae\Storage as Storage;
require_once('request.php');
// 获取新的access_token
function getNewAccess_token(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3babaf2c7f48c3d2&secret=198c5f5866b3f2fffcdc3400f5014354";
$response = httpGet($url);
// 解json串
$responseObj = json_decode($response);
//获取access_token和有效期
$access_token = $responseObj->access_token;
// 有效期
$expires_in = $responseObj->expires_in;//7200
// 当前时间 + 有效期 - 200 = 过期时间
$expires_time = time() + $expires_in - 200;
// 将过期时间和access_token转换成字符串写入storage中
$saveObj = (object)array();//创建一个对象
$saveObj->access_token = $access_token;
$saveObj->expires_time = $expires_time;
// 转成json串
$saveStr = json_encode($saveObj);
// 创建storage对象
$storage = new Storage();
// 获取bucket
// @消除用户警告
$bucket = @$storage->getBucket('bucket');//参数是bucket的名字
if ($bucket == false) {//如果bucket为空
    // 创建一个bucket  r:* 权限公开
    $bucket = $storage->putBucket("bucket","r:*");
}
// 往bucket中写入数据
// 第一个参数 要写入的数据
// 第二个参数 要写入那个bucket中
// 第三个参数 要写入那个文件
// 第四个参数 一个空数组
// 第五个参数 存入数据的文件头
$storage->putObject($saveStr,"bucket","webAccess_token.txt",array(),array('Content-type'=>'text/plain;charset=utf-8'));
return $access_token;
}
// 从storage中获取access_token
function getAccess_tokenFromStorage(){
    // 创建一个storage对象
    $storage = new Storage();
    // 获取bucket
    $bucket = @$storage->getBucket("bucket");
    if ($bucket == false) {
      // 如果没有bucket 则返回false
        return false;
    }
    // 如果有bucket
    // 先获取bucket中存储的内容  第一个参数是bucket名字  第二个参数是 存储内容的文件名
    $object = $storage->getObject("bucket","webAccess_token.txt");
    // 将存储的内容转成json串并返回
    return json_encode($object->body);
}
// 获取access_token
function getAccess_token(){
    // 先从storage中获取access_token
    $string = getAccess_tokenFromStorage();
    if ($string) {//有两种情况
        // 有 $string  json 串
        $response = json_decode($string);
        // 获取存储的对象
        $object = json_decode($response);
        // 获取存储的过期时间
        $expires_time = $object->expires_time;
        // 获取当前时间进行比较
        $time = time();
        if ($time > $expires_time) {
          //当前时间超过了过期时间 获取新的access_token
          $access_token = getNewAccess_token();
          return $access_token;
        }else {
          // 如果没有过期 就直接返回object中的access_token
            return $object->access_token;
        }
    }else {//没有
      // 如果没有bucket 就直接获取新的access_token 并创建Bucket 存储access_token
        $access_token = getNewAccess_token();
        return $access_token;
    }
}
// 获取新的票据
function  getNewTicket(){
  $access_token = getAccess_token();
  $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
  $response              = httpGet($url);
  $responseObj           = json_decode($response);
  // 票据
  $ticket                = $responseObj->ticket;
  // 过期时间
  $expires_time          = time() + $responseObj->expires_in  - 200;
  $saveObj               = (object)array();
  $saveObj->ticket       = $ticket;
  $saveObj->expires_time = $expires_time;
  $saveStr               = json_encode($saveObj);
  $storage               = new Storage();
  $bucket                = $storage->getBucket("bucket");
  $storage->putObject($saveStr,"bucket","ticket.txt",array(),array("Content-type"=>"text/plain;charset=utf8"));
  return $ticket;
}

 // 从storage中获取ticket
function getTicketFromStorage(){
    $storage = new Storage();
    // 获取bucket
    $bucket = @$storage->getBucket('bucket');
    if ($bucket == false) {
      // 如果bucket不存在 则说明ticket不存在
        return false;
    }else {
      $jsonObj = $storage->getObject("bucket","ticket.txt");
      return json_encode($jsonObj->body);
    }
}
// 获取ticket
function getTicket(){
  $response = getTicketFromStorage();
  if ($response == false) {
      $ticket = getNewTicket();
      return $ticket;
  }else {
    $responseObj = json_decode($response);
    // 获取我们存入storage中的ticket对象
    $object  = json_decode($responseObj);
    $expires_time = $object->expires_time;
    // 当前时间
    $current_time = time();
    if ($current_time > $expires_time) {
        $ticket = getNewTicket();
        return $ticket;
    }else {
      return $object->ticket;
    }
  }
}



  function getSignature(){
    $url          = $_REQUEST['url'];
    $jsapi_ticket = getTicket();
    $timestamp    = time();
    $noncestr     = getNonceStr();
    $string = "jsapi_ticket={$jsapi_ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$url}";
    $signature = sha1($string);
    // 传回数据包
    $signPack = array(
        "url"=>$url,
        "ticket"=>$jsapi_ticket,
        "appid" =>"wx336190a46d0b1f1b",
        "timestamp"=>$timestamp,
        "noncestr"=>$noncestr,
        "signature"=>$signature,
        "rawstring"=>$string
    );
    return json_encode($signPack);

  }
  echo getSignature();
  function getNonceStr(){
    // 获取30位的随机字符串
    $temp = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i=0; $i < 30 ; $i++) {
        $str .= substr($temp,mt_rand(0,strlen($temp)-1),1);
    }
    return $str;
  }
 ?>
